<?php

/**
 *  机型逻辑
*/
namespace app\admin\service;
use app\admin\model\ModelInfo as ModelInfoModel;
use app\facade\Basic;
use function Symfony\Component\String\u;

class ModelInfo extends BaseService
{


    /**
     * 获取机型列表
     */
    public function getList($param)
    {
        $where = [];
        if (!empty($param['model_name'])) {
            $where[] = ['model_name', 'like', '%' . $param['model_name'] . '%'];
        }

        $result = ModelInfoModel::with(['operator' => function ($query) {
            return $query->field('id,cname');
        }])->field('id,model_name,create_time,operator_id,latest_firmware_version,firmware_url')
            ->where($where)->order('id', 'desc')
            ->paginate([
                'list_rows'=> $param['pageSize'],
                'var_page' => 'pageIndex'
            ]);

        $data = $result->toArray();

        foreach ($data['data'] as $key => $value) {
            if ($data['data'][$key]['operator']) {
                $data['data'][$key]['operator_name'] = $value['operator']['cname'];
                unset($data['data'][$key]['operator']);
            } else {
                $data[$key]['operator_name'] = '';
            }
        }

        return Basic::paginator(0, 'ok', $data);
    }


    /**
     * save
     */
    public function save($param)
    {


        $data['operator_id'] = $param['operator_id'];
        $data['create_time'] = date('Y-m-d H:i:s',time());

        if ( !empty($param['model_name']) ) {
            $data['model_name'] = trim($param['model_name']);
        }


        if ( !empty($param['latest_firmware_version']) ) {
            $data['latest_firmware_version'] = trim($param['latest_firmware_version']);
        }

        if ( !empty($param['firmware_url']) ) {
            $data['firmware_url'] = trim($param['firmware_url']);
        }

        $result = ModelInfoModel::where('model_name', '=', $param['model_name'])->find();

        if ($result) {
            return Basic::data(1, '该机型已存在');
        }

        $result = ModelInfoModel::create($data);
        if ($result) {
            $this->logger->logOperation($result->id, 'Model', '添加', '添加机型');
            return Basic::data(0, '保存成功');
        } else {
            return Basic::data(1, '保存失败');
        }
    }


    /**
     *  修改机型
     */
    public function update($param)
    {

        if ( !empty($param['model_name']) ) {
            $param['model_name'] = trim($param['model_name']);
        }


        if ( !empty($param['latest_firmware_version']) ) {
            $param['latest_firmware_version'] = trim($param['latest_firmware_version']);
        }

        if ( !empty($param['firmware_url']) ) {
            $param['firmware_url'] = trim($param['firmware_url']);
        }

        $result = ModelInfoModel::where('model_name', '=', $param['model_name'])->where('id', '<>', $param['id'])->find();
        if ($result) {
            return Basic::data(1, '该机型已存在');
        }

        $result = ModelInfoModel::where('id', '=', $param['id'])->find();
        if(!$result){
            return Basic::data(1, '机型不存在');
        }

        $result->latest_firmware_version = $param['latest_firmware_version'];
        $result->firmware_url = $param['firmware_url'];
        $result->model_name = $param['model_name'];
        $result->operator_id = $param['operator_id'];
        if ($result->save()) {
            $this->logger->logOperation($result->id, 'Model', '修改', '修改机型');
            return Basic::data(0, '修改成功');
        } else {
            return Basic::data(1, '修改失败');
        }

    }


    /**
     *  获取详情
    */
    public function detail($param)
    {

        $result = ModelInfoModel::with(['operator'=>function($query){
            return $query->field('id,cname');
        }])->field('id,model_name,create_time,operator_id,latest_firmware_version,firmware_url')->where('id','=',$param['id'])->find();

        if ( $result ) {
            $result->operator_name = $result->operator['cname'];
            unset($result->operator);
            return Basic::data(0, 'ok', $result);
        } else {
            return Basic::data(1, '获取失败');
        }
    }

    /**
     *   删除机型
    */
    public function delete($param)
    {
        $result = ModelInfoModel::where('id','in',$param['ids'])->find();
        if ( $result ) {
            if ( ModelInfoModel::where('id','in',$param['ids'])->delete()) {
                $this->logger->logOperation($param['ids'], 'Model', '修改', '删除机型');
                return Basic::data(0, '删除成功');
            } else {
                return Basic::data(1, '删除失败');
            }
        } else {
            return Basic::data(1, '删除失败');
        }
    }


}
